package com.sz.wechat.controller;

import com.sz.wechat.entity.User;
import com.sz.wechat.mapper.UserMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理", description = "用户相关的API")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Operation(summary = "获取所有用户", description = "获取所有用户信息", tags = {"用户管理"})
    @GetMapping
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Operation(summary = "获取用户", description = "根据用户ID获取用户信息", tags = {"用户管理"})
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userMapper.selectById(id);
    }

    @Operation(summary = "创建用户", description = "创建新用户", tags = {"用户管理"})
    @PostMapping
    public User createUser(@RequestBody User user) {
        userMapper.insert(user);
        return user;
    }

    @Operation(summary = "更新用户", description = "根据用户ID更新用户信息", tags = {"用户管理"})
    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userMapper.updateById(user);
        return user;
    }

    @Operation(summary = "删除用户", description = "根据用户ID删除用户", tags = {"用户管理"})
    @DeleteMapping("/{id}")
    public int deleteUser(@PathVariable Long id) {
        return userMapper.deleteById(id);
    }
}